package jmp123.shell.xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import jmp123.shell.vo.Group;
import jmp123.shell.vo.Song;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlKit {

	public static String	path	= "player.xml";

	public static List<Group> loadGroups(String path) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(path);
			Document document = builder.parse(f);
			NodeList list = document.getElementsByTagName("group");
			List<Group> groups = new ArrayList<Group>();
			for (int i = 0; i < list.getLength(); i++) {
				Node node = list.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element ele = (Element) node;
					String name = ele.getAttribute("name");

					if (name != null) {
						Group group = new Group();
						group.setName(name);
						groups.add(group);

						NodeList nl = ele.getChildNodes();
						for (int j = 0; j < nl.getLength(); j++) {
							Node n = nl.item(j);
							if (n.getNodeType() == Node.ELEMENT_NODE) {
								Element e = (Element) n;

								String p = e.getTextContent().trim();
								if (p != null) {
									Song song = new Song();
									song.setPath(p);
									group.addSong(song);
								}
							}
						}
					}
				}
			}
			return groups;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

	public static void saveGroups(List<Group> groups, String path) {
		if (groups == null || groups.isEmpty()) {
			return;
		}

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(path);
			Document document = builder.newDocument();
			Element root = document.getDocumentElement();
			if (root == null) {
				root = document.createElement("player");
				document.appendChild(root);
			}
			Element parent = null;
			for (Node n = root.getFirstChild(); n != null; n = n
					.getNextSibling()) {
				if (n.getNodeType() == Node.ELEMENT_NODE) {
					if (n.getNodeName().equalsIgnoreCase("groups")) {
						parent = (Element) n;
						break;
					}
				}
			}
			if (parent == null) {
				parent = document.createElement("groups");
				root.appendChild(parent);
			}
			for (Node n = parent.getFirstChild(); n != null; n = n
					.getNextSibling()) {
				parent.removeChild(n);
			}
			for (Group group : groups) {
				Element ele = document.createElement("group");
				ele.setAttribute("name", group.getName());
				parent.appendChild(ele);

				for (Song song : group.listSongs()) {
					Element e = document.createElement("song");
					e.setTextContent(song.getPath());
					ele.appendChild(e);
				}
			}
			DOMSource doms = new DOMSource(root);
			Transformer tf = TransformerFactory.newInstance().newTransformer();
			tf.setOutputProperty(OutputKeys.INDENT, "yes");
			tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			tf.transform(doms, new StreamResult(f));
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
}
